

//
//  NewCouponsViewController.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/5/23.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "NewCouponsViewController.h"
#import "NewCouponsCell.h"
#import "UserInfoModel.h"
#import "CanReceiveCoupnsListModel.h"

NSString *const NewCouponsCellIdentifier = @"NewCouponsCellIdentifier";
@interface NewCouponsViewController ()
@property(nonatomic ,strong) CanReceiveCoupnsListModel * listModel;
@end

@implementation NewCouponsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationItemTitle: @"优惠券"];
 
}
- (void)requestUserCoupon{
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Coupon/getList" params:@{@"token":UserInfo.userToken ,@"p":@(self.currentPageNo),@"psize":@(self.pageCount)} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            if (strongSelf.currentPageNo == 0) {
                strongSelf.listModel = nil;
                strongSelf.listModel = [[CanReceiveCoupnsListModel alloc]initWithDictionary:responseObject error:nil];
                  strongSelf.currentPageNo ++;
            }else{
                if (responseObject[@"data"] && [responseObject[@"data"] count] >0) {
                    [strongSelf.listModel.data addObjectsFromArray: responseObject[@"data"]];
                      strongSelf.currentPageNo ++;
                }
            }
         
        }
        [strongSelf updateTableView];
        NSLog(@"%@===",responseObject);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NewCouponsCell class]) bundle:nil] forCellReuseIdentifier:NewCouponsCellIdentifier];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    NSInteger sections = [self.listModel.data count];
     
    return sections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rows = 1;
    
    return rows;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 94;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    
    NewCouponsCell * tempCell = [tableView dequeueReusableCellWithIdentifier:NewCouponsCellIdentifier];
    CanReceiveCoupnsModel *model = self.listModel.data[indexPath.section];
    [tempCell  setupCouponsInfo:model];
    cell = tempCell;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     CanReceiveCoupnsModel *model = self.listModel.data[indexPath.section];
    [self requestReceiveCoupon:model.coupon_id];
}
- (void)requestReceiveCoupon:(NSNumber *)coupon_id{
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Coupon/receive" params:@{@"token":UserInfo.userToken ,@"coupon_id":coupon_id} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            [strongSelf showAlertView:@"优惠券领取成功" withConfirmAction:^(UIAlertAction *action) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"UPDETA_COUPONS_LIST" object:nil];
                [self requestUserCoupon];
            }];
        }
            
    }];
        
}
- (BOOL)isAddRefreshHeader{
    return YES;
}
- (BOOL)isAddRefreshFooter{
    return YES;
}
- (NSInteger )getNetworkTableViewDataCount{

    return [self.listModel.data count];
}
- (void)getLoadMoreTableViewNetworkData{
    
    [self requestUserCoupon];
}
- (void)getNormalTableViewNetworkData{
    self.currentPageNo = 0;
    self.pageCount = 10;
 
    [self  requestUserCoupon];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
